﻿namespace Offy.Itdev.BuyIn.Intranet.Services.ServiceLocator
{
    using System;
    using System.Collections.Generic;

    public class ServiceLocator : IServiceLocator
    {
        private static Dictionary<Type, object> services = null;
        private static readonly object TheLock = new Object();
        private static IServiceLocator instance;

        public ServiceLocator()
        {
            services = new Dictionary<Type, object>();
        }

        public void Register<T>(T service)
        {
            services[typeof(T)] = service;
        }

        public T Resolve<T>()
        {
            return (T) services[typeof (T)];
        }

        public static IServiceLocator Instance
        {
            get
            {
                lock (TheLock) // thread safety
                {
                    if (instance == null) instance = new ServiceLocator();
                }
                return instance;
            }
        }
    }
}
